package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

public class SecondFilterHandler implements ResourceFilterHandler{
    private ResourceFilterHandler nextHanlder;
    public void setNextHanlder(ResourceFilterHandler nextHanlder) {
        this.nextHanlder = nextHanlder;
    }
    @Override
    public void filter(List<Resource> listResource, TreeItemVo root) {
        //5. 遍历每一个一级目录 然后找找对应的一级目录下的二级目录 并构建关系
        root.getChildren().stream().map(first -> {
            List<Resource> secondTree = listResource.stream().filter(r -> {
                if (r.getParentResourceNo().equals(first.getId())) {
                    return true;
                } else {
                    return false;
                }
            }).collect(Collectors.toList());

            List<TreeItemVo> secondTreeList = secondTree.stream().map(r -> {
                TreeItemVo vo = new TreeItemVo();
                vo.setId(r.getResourceNo());
                vo.setLabel(r.getResourceName());
                return vo;
            }).collect(Collectors.toList());

            first.setChildren(secondTreeList);

            return first;
        }).collect(Collectors.toList());

        if(null != nextHanlder){
            nextHanlder.filter(listResource, root);
        }
    }
}
